import 'package:flutter/cupertino.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/views/home/view/order/model/sales_order.dart';

import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../utils/common_req_utils.dart';
import '../../../../widget/label_item.dart';
import '../../../../widget/w_toast.dart';

class SalesOrderDetailPage extends StatefulWidget {
  final String? orderCode;

  const SalesOrderDetailPage({super.key, this.orderCode});

  @override
  BaseState<SalesOrderDetailPage> createState() => SalesOrderDetailPageStata();
}

class SalesOrderDetailPageStata extends BaseState<SalesOrderDetailPage> {

  SalesOrder salesOrder = SalesOrder();

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  void fetchData() {
    showLoading();
    HttpUtils().get("${Api.getSalesOrderDetail}/${widget.orderCode}").then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        if(value.data != null) {
          salesOrder = SalesOrder.fromJson(value.data);
          setState(() {});
        }
      } else {
        Toast.toast(value.errMsg);
      }
    });
  }

  @override
  FullBasePage buildState(BuildContext context) {

    String payMethod = "";
    if ( salesOrder.payMethod == 10241001) {
      payMethod = "全款";
    } else if (salesOrder.payMethod == 10241002) {
      payMethod = "按揭";
    }

    return FullBasePage(
        title: "销售订单信息",
        child: ListView(
            padding: EdgeInsets.zero,
            children: [
              LabelItem(label: "销售单号", value: salesOrder.orderCode,),
              LabelItem(label: "销售订单状态", value: CommonReqUtils.exchangeCodeToName(
                  CommonReqUtils.storeCarStatus, salesOrder.status),),
              LabelItem(label: "客户", value: salesOrder.custName,),
              LabelItem(label: "性别", value: CommonReqUtils.exchangeCodeToName(
                  CommonReqUtils.sex, salesOrder.sex),),
              LabelItem(label: "手机号码", value: salesOrder.phone,),
              LabelItem(label: "客户类型", value: CommonReqUtils.exchangeCodeToName(
                  CommonReqUtils.customerType, salesOrder.customerType),),
              LabelItem(label: "公司名称", value: salesOrder.companyName,),
              LabelItem(label: "证件类型", value: CommonReqUtils.exchangeCodeToName(
                  CommonReqUtils.cardType, salesOrder.idType),),
              LabelItem(label: "证件号码", value: salesOrder.idNo,),
              LabelItem(label: "VIN", value: salesOrder.vin,),
              LabelItem(label: "车型", value: salesOrder.modelName,),
              LabelItem(label: "版本", value: salesOrder.versionName,),
              LabelItem(label: "外观", value: salesOrder.outColor,),
              LabelItem(label: "内饰", value: salesOrder.inColor,),
              LabelItem(label: "官方指导价",value: salesOrder.standardPrice == null ? "" : salesOrder.standardPrice.toString(),),
              LabelItem(label: "抵扣价", value: salesOrder.deductionPrice == null ? "" : salesOrder.deductionPrice.toString(),),
              LabelItem(label: "市场零售价", value: salesOrder.vehiclePrice == null ? "" : salesOrder.vehiclePrice.toString(),),
              LabelItem(label: "定金金额", value: salesOrder.depositAmount == null ? "" : salesOrder.depositAmount.toString(),),
              LabelItem(label: "尾款", value: salesOrder.unpaidAmount == null ? "" : salesOrder.unpaidAmount.toString(),),
              LabelItem(label: "购车方式", value: payMethod,),
              LabelItem(label: "直营店名称", value: salesOrder.companyName,),
              LabelItem(label: "交付中心", value: salesOrder.centerShopName,),
              LabelItem(label: "提车日期", value: salesOrder.deliveryDate,),
              LabelItem(label: "订单备注", value: salesOrder.remark,labelType: LabelType.TEXTAREA,
                isEnabled: false,),
              LabelItem(label: "订单创建时间", value: salesOrder.createDate,),
              LabelItem(label: "是否判单", value: CommonReqUtils.exchangeCodeToName(
                  CommonReqUtils.yesOrNo, salesOrder.isJudge),),
              LabelItem(label: "归属手机号码", value: salesOrder.belongPhone,),
              LabelItem(label: "归属直营店", value: salesOrder.belongShopName,),
              LabelItem(label: "归属销售顾问", value: salesOrder.belongSalemanName,),
              LabelItem(label: "判单人", value: salesOrder.judgeUserName,),
              LabelItem(label: "判单时间", value: salesOrder.judgeDate,),
              LabelItem(label: "判单备注", value: salesOrder.judgeRemake,labelType: LabelType.TEXTAREA,
                isEnabled: false,),
            ]
        )
    );
  }

}